package org.gruposp2p.dnie.service.jpa;

import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.gruposp2p.dnie.model.DNIeDocumentToSign;
import org.gruposp2p.dnie.model.DNIeDocumentToSignOption;
import org.gruposp2p.dnie.model.DNIeUser;
import org.gruposp2p.dnie.service.DNIeDocumentToSignDAO;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
/**
 *
 * @author jj
 */
@Repository("documentToSignDao")
public class JpaDNIeDocumentToSignDAO implements DNIeDocumentToSignDAO {

    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
            this.entityManager = entityManager;
    }

    public DNIeDocumentToSign findById(Integer id) {
        return entityManager.find(DNIeDocumentToSign.class, id);
    }

    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
    public List<DNIeDocumentToSign> findList(int firstResult, int maxResults) {
        Query query = entityManager.createQuery(
                "SELECT d FROM DNIeDocumentToSign d ORDER BY dateCreated DESC");
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResults);
        List<DNIeDocumentToSign> documentsToSign = query.getResultList();
        return documentsToSign;
    }
    
    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
    public List<DNIeDocumentToSign> findUserList(
    		int firstResult, int maxResults, DNIeUser dnieUserId) {
        Query query = entityManager.createQuery(
                "SELECT d FROM DNIeDocumentToSign d WHERE d.dnieUserId = :dnieUserId ORDER BY dateCreated DESC");
        query.setParameter("dnieUserId", dnieUserId);
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResults);
        List<DNIeDocumentToSign> documentsToSign = query.getResultList();
        return documentsToSign;
    }

    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
    public DNIeDocumentToSign save(DNIeDocumentToSign documentToSign) {
    	Collection<DNIeDocumentToSignOption> options =
    		documentToSign.getDNIeDocumentToSignoptionCollection();
    	if (options != null) {
            for (DNIeDocumentToSignOption option : options) {
                option.setDNIeDocumentToSignId(documentToSign);
            }
    	}
        entityManager.persist(documentToSign);
        entityManager.flush();
        return documentToSign;
    }

    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
    public DNIeDocumentToSign update(DNIeDocumentToSign documentToSign) {
        //DNIeDocumentToSign dbDNIeDocumentToSign =
        //(DNIeDocumentToSign)getObjectForPK(DNIeDocumentToSign.class, dnieDocumentToSign.getId());
        Collection<DNIeDocumentToSignOption> options =
    		documentToSign.getDNIeDocumentToSignoptionCollection();
    	if (options != null) {
            for (DNIeDocumentToSignOption option : options) {
                option.setDNIeDocumentToSignId(documentToSign);
            }
    	}
        entityManager.merge(documentToSign);
        entityManager.flush();
        return documentToSign;
    }

    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
    public boolean delete(Integer documentId) {
        DNIeDocumentToSign documentToSign =
                entityManager.getReference(DNIeDocumentToSign.class, documentId);
        if (documentToSign == null)
                return false;
        entityManager.remove(documentToSign);
        entityManager.flush();
        return true;
    }

    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
	public List<DNIeDocumentToSign> findOrderedByNumberSignaturesList(
			int firstResult, int maxResults) {
        Query query = entityManager.createQuery(
        	"SELECT d FROM DNIeDocumentToSign d ORDER BY numberSignatures DESC");
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResults);
        List<DNIeDocumentToSign> documentsToSign = query.getResultList();
        return documentsToSign;
	}

}
